package dhm.com.dhmshop.view.mine.myshop;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.MonthShopProfit;
import dhm.com.dhmshop.utils.SpUtils;

public class ChoosetimeActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.start_text)
    TextView startText;
    @BindView(R.id.start_line)
    TextView startLine;
    @BindView(R.id.end_text)
    TextView endText;
    @BindView(R.id.end_line)
    TextView endLine;
    @BindView(R.id.month)
    LinearLayout month;
    @BindView(R.id.day_text)
    TextView dayText;
    @BindView(R.id.day)
    LinearLayout day;
    private String uid;
    private int shop_id;

    private PressenterImpl pressenter;
    private MonthShopProfit monthShopProfit;

    @Override
    protected int getLayout() {
        return R.layout.activity_choosetime;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        uid = SpUtils.getString(this, "uid");
        shop_id = getIntent().getIntExtra("shop_id", 1);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.back, R.id.finish, R.id.time, R.id.start_data, R.id.end_data, R.id.day})
    public void onViewClicked(View view) {
        TimePickerView pickerView;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.finish:
                if (time.getText().toString().equals("按日选择")){
                    if (startText.getText().toString().equals("请选择")||endText.getText().toString().equals("请选择")){
                        Toast.makeText(this, "请选择", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Map<String,String> map=new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid",uid);
                    map.put("shop_id",shop_id+"");
                    map.put("from_date",startText.getText().toString());
                    map.put("to_date",endText.getText().toString());
                    pressenter.sendMessage(ChoosetimeActivity.this,Constant.SectionShopProfit,map, MonthShopProfit.class);


                }else {
                    if (dayText.getText().toString().equals("请选择")){
                        Toast.makeText(this, "请选择", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Map<String,String> map=new HashMap<>();
                    map.put("token", Constant.TOKEN);
                    map.put("uid",uid);
                    map.put("shop_id",shop_id+"");
                    map.put("month",dayText.getText().toString());
                    pressenter.sendMessage(ChoosetimeActivity.this,Constant.MonthShopProfit,map, MonthShopProfit.class);
                }
                break;
            case R.id.time:
                if (time.getText().toString().equals("按月选择")){
                    time.setText("按日选择");
                    day.setVisibility(View.GONE);
                    month.setVisibility(View.VISIBLE);
                }else {
                    time.setText("按月选择");
                    day.setVisibility(View.VISIBLE);
                    month.setVisibility(View.GONE);
                }
                break;
            case R.id.start_data:
                //开始某一天
                pickerView = new TimePickerBuilder(ChoosetimeActivity.this,
                        new OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date, View v) {
                                startText.setText(getTimeDay(date));
                            }
                        }).setCancelColor(R.color.main).setSubmitColor(R.color.main).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                pickerView.show(view);
                break;
            case R.id.end_data:
                //结束某一天
                pickerView = new TimePickerBuilder(ChoosetimeActivity.this,
                        new OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date, View v) {
                                endText.setText(getTimeDay(date));
                            }
                        }).setCancelColor(R.color.main).setSubmitColor(R.color.main).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                pickerView.show(view);
                break;
            case R.id.day:
                //某个月
                pickerView = new TimePickerBuilder(ChoosetimeActivity.this,
                        new OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date, View v) {
                                dayText.setText(getTimeMonth(date));
                            }
                        }).setCancelColor(R.color.main).setSubmitColor(R.color.main).setType(new boolean[]{true, true, true, false, false, false})
                        .build();
                pickerView.show(view);
                break;
                default:
        }
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return format.format(date);
    }
    private String getTimeMonth(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
        return format.format(date);
    }
    private String getTimeDay(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return format.format(date);
    }


    @Override
    public void requesta(Object data) {
        if (data instanceof MonthShopProfit){
            monthShopProfit = (MonthShopProfit) data;
            if (monthShopProfit.getData().get(0).getTotal()!=null){
                Intent intent = new Intent();
                intent.putExtra("total", monthShopProfit.getData().get(0).getTotal());
                if (time.getText().toString().equals("按日选择")){
                    intent.putExtra("time",startText.getText().toString()+"n"+endText.getText().toString());
                }else {
                    intent.putExtra("time",dayText.getText().toString());
                }
                // 给上一个活动返回数据
                setResult(RESULT_OK, intent);// 回调(FirstActivity.java中)onActivityResult方法
                finish();
            }else {
                Intent intent = new Intent();
                intent.putExtra("total", "0");
                // 给上一个活动返回数据
                if (time.getText().toString().equals("按日选择")){
                    intent.putExtra("time",startText.getText().toString()+"\n"+endText.getText().toString());
                }else {
                    intent.putExtra("time",dayText.getText().toString());
                }
                setResult(RESULT_OK, intent);// 回调(FirstActivity.java中)onActivityResult方法
                finish();

            }

        }

    }

    @Override
    public void fail(String error) {

    }
}
